<?php

namespace Api\Controller;

/**
 * Class UserController
 * @package Api\Controller
 */
class UserController extends PublicController
{
    private $userId;

    public function _initialize()
    {
        parent::_initialize();
        $userId = intval($this->get("id"));
        if (!$userId) {
            $this->json_error("请登录");
        }

        $this->userId = $userId;
    }

    /**
     * 获取用户信息
     */
    public function getMyInfo()
    {
        $user = M("user")->where('id=' . $this->userId)->find();
        if ($user['photo'] && !strstr($user['photo'], "https://")) {
            $user['photo'] = __DATAURL__ . $user['photo'];
        }
        $this->json_result($user);
    }


    /**
     * 修改用户资料
     * @author tiedong
     * @date 2018-01-17
     */
    public function updateUserInfo()
    {
        $data = [];
        isset($_POST['name']) && $data['name'] = $_POST['name'];
        isset($_POST['sex']) && $data['sex'] = intval($_POST['sex']);
        isset($_POST['birth']) && $data['birth'] = $_POST['birth'];
        isset($_POST['address']) && $data['address'] = $_POST['address'];
        isset($_POST['photo']) && $data['photo'] = $_POST['photo'];
        isset($_POST['tel']) && $data['tel'] = $_POST['tel'];
        isset($_POST['card']) && $data['card'] = $_POST['card'];

        $result = null;
        !empty($data) && $result = M("user")->where('id=' . $this->userId)->save($data);
        if ($result) {
            $this->json_result("操作成功！");
        } else {
            $this->json_error("操作失败");
        }
    }

    /**
     * 我的积分记录
     */
    public function pointLog()
    {
        $perPage = !empty($_GET['per_page']) ? intval($_GET['per_page']) : 10;
        $curPage = !empty($_GET['page']) ? intval($_GET['page']) : 1;
        $where = "user_id=" . $this->userId;
        $count = M("point_log")->where($where)->count();
        $totalPage = ceil($count / $perPage);
        $list = M("point_log")->where($where)->order('id desc')->limit(($curPage - 1) * $perPage, $perPage)->select();

        $this->json_result([
            'per_page'   => $perPage,
            'total'      => $count,
            'total_page' => $totalPage,
            'cur_page'   => $curPage,
            'list'       => $list,
        ]);
    }

    /**
     * 获取地址列表
     */
    public function addressList()
    {
        $list = M('address')->where('uid=' . $this->userId)->order('is_default desc,id desc')->select();
        $this->json_result($list);
    }

    /**
     * 获取具体地址，无id时获取默认地址
     */
    public function address()
    {
        if (isset($_GET['id']) && $id = intval($_GET['id'])) {
            $detail = M('address')->where('id = ' . $id . ' and uid = ' . $this->userId)->find();
        } else {
            $detail = M('address')->where('is_default = 1 and uid = ' . $this->userId)->find();
        }
        $this->json_result($detail);
    }

    /**
     * 会员添加地址
     * @author tiedong
     * @date 2018-01-23
     */
    public function addAddress()
    {
        if (!$_POST['name'] || !$_POST['mobile'] || !$_POST['address'] || empty($_POST['region_name'])) {
            $this->json_error("请完善信息后再提交！！！");
        }
        $data = [
            'name'        => $_POST['name'],
            'mobile'      => $_POST['mobile'],
            'region_name' => $_POST['region_name'],
            'address'     => $_POST['address'],
            'uid'         => $this->userId
        ];

        $check_id = M('address')->where($data)->select();
        if ($check_id) {
            $this->json_error("该地址已经存在");
        }
        $data['is_default'] = intval($_POST['is_default']);
        if ($data['is_default']) {
            M('address')->where(['uid' => $this->userId])->save(['is_default' => 0]);
        }
        $res = M('address')->add($data);
        if ($res) {
            $this->json_result(['id' => $res], "添加地址成功！");
        } else {
            $this->json_error("添加失败！");
        }
    }

    /**
     * 修改地址
     */
    public function updateAddress()
    {
        $id = intval($_POST['id']);
        if (empty($id)) {
            $this->json_error('id 参数不能为空');
        }

        $data = [];
        !empty($_POST['name']) && $data['name'] = $_POST['name'];
        !empty($_POST['mobile']) && $data['mobile'] = $_POST['mobile'];
        !empty($_POST['region_name']) && $data['region_name'] = $_POST['region_name'];
        !empty($_POST['address']) && $data['address'] = $_POST['address'];
        isset($_POST['is_default']) && $data['is_default'] = intval($_POST['is_default']);

        if (isset($data['is_default']) && $data['is_default'] == 1) {
            M('address')->where(['uid' => $this->userId])->save(['is_default' => 0]);
        }

        !empty($data) && $result = M('address')->where(['id' => $id, 'uid' => $this->userId])->save($data);

        if (empty($result)) {
            $this->json_error("操作失败！");
        } else {
            $this->json_result("操作成功！");
        }
    }

    /**
     * 删除地址
     */
    public function delAddress()
    {
        $id = (int)$_GET['id'];
        if (empty($id)) {
            $this->json_error('id 参数不能为空');
        }

        M('address')->where(['id' => $id, 'uid' => $this->userId])->delete();
        $this->json_result("操作成功！");
    }

    /**
     * 转发送积分
     */
    public function getPointByZF()
    {
        $today = date('Y-m-d') . ' 00:00:00';
        $times = M('point_log')->where("user_id = {$this->userId} and type = 5 and create_at > '$today'")->count();
        if ($times < 6) {
            // 转发 + 15 积分
            M('user')->where(['id' => $this->userId])->setInc('point', 15);
            M('point_log')->add(['user_id' => $this->userId, 'point' => 15, 'type' => 5, 'remark' => '转发送积分', 'create_at' => date('Y-m-d H:i:s')]);
        }
        $this->json_result("操作成功！");
    }
}
